<page-header title="'Users'" breadcrumbs="['User management']" reload="true"> </page-header>

<div class="row" ng-if="isAdmin">
  <div class="col-lg-12 col-md-12 col-xs-12">
    <rd-widget>
      <rd-widget-header icon="plus" title-text="Add a new user"> </rd-widget-header>
      <rd-widget-body>
        <form name="form" class="form-horizontal">
          <!-- name-input -->
          <div class="form-group">
            <label for="username" class="col-sm-3 col-lg-2 control-label required text-left">
              Username
              <portainer-tooltip ng-if="AuthenticationMethod === 2" message="'Username must exactly match username defined in external LDAP source.'"></portainer-tooltip>
            </label>
            <div class="col-sm-8">
              <div class="input-group">
                <input
                  type="text"
                  data-cy="user-usernameInput"
                  class="form-control"
                  id="username"
                  ng-model="formValues.Username"
                  ng-change="checkUsernameValidity()"
                  placeholder="e.g. jdoe"
                  auto-focus
                />
                <span class="input-group-addon">
                  <pr-icon mode="'success'" icon="'check'" ng-if="state.validUsername"></pr-icon>
                  <pr-icon mode="'danger'" icon="'x'" ng-if="!state.validUsername"></pr-icon>
                </span>
              </div>
            </div>
          </div>
          <!-- !name-input -->
          <!-- new-password-input -->
          <div class="form-group" ng-if="AuthenticationMethod === 1">
            <label for="password" class="col-sm-3 col-lg-2 control-label required text-left">Password</label>
            <div class="col-sm-8">
              <input
                type="password"
                class="form-control"
                ng-model="formValues.Password"
                id="password"
                name="password"
                ng-minlength="requiredPasswordLength"
                data-cy="user-passwordInput"
              />
            </div>
          </div>
          <!-- !new-password-input -->
          <!-- confirm-password-input -->
          <div class="form-group" ng-if="AuthenticationMethod === 1">
            <label for="confirm_password" class="col-sm-3 col-lg-2 control-label required text-left">Confirm password</label>
            <div class="col-sm-8">
              <div class="input-group">
                <input
                  type="password"
                  class="form-control form-control--has-icon"
                  ng-model="formValues.ConfirmPassword"
                  id="confirm_password"
                  data-cy="user-passwordConfirmInput"
                />
                <span class="input-group-addon">
                  <pr-icon
                    mode="'success'"
                    icon="'check'"
                    aria-hidden="true"
                    ng-if="form.password.$viewValue !== '' && form.password.$viewValue === formValues.ConfirmPassword"
                  ></pr-icon>
                  <pr-icon
                    mode="'danger'"
                    icon="'x'"
                    aria-hidden="true"
                    ng-if="!(form.password.$viewValue !== '' && form.password.$viewValue === formValues.ConfirmPassword)"
                  ></pr-icon>
                </span>
              </div>
            </div>
          </div>
          <!-- !confirm-password-input -->

          <!-- password-check-hint -->
          <div class="form-group" ng-if="AuthenticationMethod === 1">
            <div class="col-sm-3 col-lg-2"></div>
            <div class="col-sm-8">
              <password-check-hint password-valid="form.password.$valid && formValues.Password"></password-check-hint>
            </div>
          </div>
          <!-- ! password-check-hint  -->

          <!-- admin-checkbox -->
          <div class="form-group" ng-if="isAdmin">
            <div class="col-sm-12">
              <por-switch-field
                label-class="'col-sm-3 col-lg-2 control-label text-left'"
                checked="formValues.Administrator"
                label="'Administrator'"
                tooltip="'Administrators have access to Portainer settings management as well as full control over all defined environments and their resources.'"
                on-change="(handleAdministratorChange)"
              ></por-switch-field>
            </div>
          </div>
          <!-- !admin-checkbox -->
          <!-- teams -->
          <div class="form-group" ng-if="!formValues.Administrator">
            <label class="col-sm-3 col-lg-2 control-label text-left" for="teams-selector">Add to team(s)</label>
            <div class="col-sm-8">
              <span class="small text-muted" ng-if="teams.length === 0">
                You don't seem to have any teams to add users into. Head over to the <a ui-sref="portainer.teams">Teams view</a> to create some.
              </span>
              <teams-selector
                ng-if="teams.length > 0"
                value="formValues.TeamIds"
                teams="teams"
                placeholder="'Select one or more teams'"
                data-cy="'user-teamSelect'"
                on-change="(onChangeTeamIds)"
                input-id="'teams-selector'"
                disabled="teamSync"
              ></teams-selector>
            </div>
          </div>
          <div class="form-group" ng-if="teamSync">
            <div class="col-sm-12">
              <span class="small">
                <p class="small text-muted vertical-center">
                  <pr-icon icon="'alert-circle'" class-name="'icon-warning =vertical-center'"></pr-icon>
                  The team leader feature is disabled as external authentication is currently enabled with team sync.
                </p>
              </span>
            </div>
          </div>
          <!-- !teams -->
          <div class="form-group" ng-if="isAdmin && !formValues.Administrator && formValues.Teams.length === 0">
            <div class="col-sm-12">
              <span class="small text-muted vertical-center">
                <pr-icon class="vertical-center" icon="'alert-circle'" aria-hidden="true" mode="'primary'" size="'md'"></pr-icon>
                <span
                  >Note: non-administrator users who aren't in a team don't have access to any environments by default. Head over to the
                  <a ui-sref="portainer.endpoints">Environments view</a> to manage their accesses.</span
                >
              </span>
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-12">
              <button
                type="button"
                class="btn btn-primary btn-sm !ml-0"
                ng-disabled="state.actionInProgress || !state.validUsername || formValues.Username === '' || (AuthenticationMethod === 1 && (!formValues.Password || form.$invalid || formValues.Password !== formValues.ConfirmPassword))"
                ng-click="addUser()"
                button-spinner="state.actionInProgress"
                data-cy="user-createUserButton"
              >
                <span ng-hide="state.actionInProgress" class="vertical-center icon-white"> <pr-icon icon="'plus'" aria-hidden="true" size="'md'"></pr-icon> Create user</span>
                <span ng-show="state.actionInProgress">Creating user...</span>
              </button>
              <span class="text-danger" ng-if="state.userCreationError">
                <pr-icon icon="'alert-circle'" aria-hidden="true" mode="'primary'" size="'md'"></pr-icon> {{ state.userCreationError }}
              </span>
            </div>
          </div>
        </form>
      </rd-widget-body>
    </rd-widget>
  </div>
</div>

<users-datatable dataset="users" on-remove="(removeAction)"></users-datatable>
